package com.lm.guava.controller;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    private final RateLimiter rateLimiter;

    public DemoController(RateLimiter rateLimiter) {
        this.rateLimiter = rateLimiter;
    }

    @GetMapping("/api/hello")
    public String hello() {
        // 尝试获取令牌（非阻塞）
        if (!rateLimiter.tryAcquire()) {
            return "请求过多，请稍后再试！";
        }
        return "Hello, 请求成功！";
    }
}